package cn.akwangl.concurrency.web.model.order.service.impl;

import cn.akwangl.concurrency.web.common.lock.ZkLock;
import cn.akwangl.concurrency.web.model.order.pojo.OrderVo;
import cn.akwangl.concurrency.web.model.order.pojo.SubmitOrderDto;
import cn.akwangl.concurrency.web.model.order.service.CreateOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * zookeeper 方式解决线程安全问题.
 *
 * @author 阿坤同学
 * @since 2022-11-22 14:29:53
 */
@Service("zkOrdersService")
public class ZkOrdersServiceImpl implements CreateOrdersService {

    private CreateOrdersService createOrdersService;

    @Autowired
    @Qualifier("createOrdersService")
    public void setCreateOrdersService(CreateOrdersService createOrdersService) {
        this.createOrdersService = createOrdersService;
    }


    @Override
    public OrderVo createOrder(SubmitOrderDto submitOrderDto) {
        try (ZkLock zkLock = new ZkLock()) {
            zkLock.getLock("order");
            return createOrdersService.createOrder(submitOrderDto);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
